package com.x.chat.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Value("${image-path}")
    private String imagePath;

    @PostMapping("/uploadImage")
    public String sendMessage(@RequestParam("file") MultipartFile file) {
        File imageStorePath = new File(imagePath);
        if (!imageStorePath.exists()) {
            imageStorePath.mkdirs();
        }
        try {
            String imageId = UUID.randomUUID().toString();
            File imageStoreFile = new File(imagePath + File.separator + imageId);
            file.transferTo(imageStoreFile);

            return "getImage/" + imageId;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
